package com.canaan.business.service.bill.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.domain.vo.OutStockQueryVo;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.bill.TrayProductFreezeMapper;
import com.canaan.business.domain.bill.TrayProductFreeze;
import com.canaan.business.service.bill.ITrayProductFreezeService;
import com.canaan.common.annotation.TableName;
/**
 * 托盘物料冻结Service业务层处理
 *
 * @author canaan
 * @date 2025-04-21
 */
@Service
public class TrayProductFreezeServiceImpl implements ITrayProductFreezeService {
    @Resource
    private TrayProductFreezeMapper trayProductFreezeMapper;

    /**
     * 查询托盘物料冻结
     *
     * @param id 托盘物料冻结主键
     * @return 托盘物料冻结
     */
    @Override
    public TrayProductFreeze selectTrayProductFreezeById(Long id) {
        return trayProductFreezeMapper.selectTrayProductFreezeById(id);
    }

    /**
     * 查询托盘物料冻结列表
     *
     * @param trayProductFreeze 托盘物料冻结
     * @return 托盘物料冻结
     */
    @Override
    public List<TrayProductFreeze> selectTrayProductFreezeList(TrayProductFreeze trayProductFreeze) {
        return trayProductFreezeMapper.selectTrayProductFreezeList(trayProductFreeze);
    }

    /**
     * 新增托盘物料冻结
     *
     * @param trayProductFreeze 托盘物料冻结
     * @return 结果
     */
    @Override
    public int insertTrayProductFreeze(TrayProductFreeze trayProductFreeze) {
        if(StringUtils.isBlank(trayProductFreeze.getNo())){
            trayProductFreeze.setNo(TableNoUtil.getNo(TrayProductFreeze.class));
        }
        trayProductFreeze.setCreateBy(SecurityUtils.getUsername());
        trayProductFreeze.setCreateTime(DateUtils.getNowDate());
        return trayProductFreezeMapper.insertTrayProductFreeze(trayProductFreeze);
    }

    /**
     * 批量新增托盘物料冻结
     *
     * @param trayProductFreezeList 托盘物料冻结列表
     * @return 结果
     */
    @Override
    public int batchInsertTrayProductFreeze(List<TrayProductFreeze> trayProductFreezeList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayProductFreeze trayProductFreeze : trayProductFreezeList){
            if(StringUtils.isBlank(trayProductFreeze.getNo())){
                trayProductFreeze.setNo(TableNoUtil.getNo(TrayProductFreeze.class));
            }
            trayProductFreeze.setCreateBy(username);
            trayProductFreeze.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<trayProductFreezeList.size();) {
            int endIndex = i+500;
            if (endIndex > trayProductFreezeList.size()) {
                endIndex = trayProductFreezeList.size();
            }
            insertNum = insertNum + trayProductFreezeMapper.batchInsertTrayProductFreeze(trayProductFreezeList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改托盘物料冻结
     *
     * @param trayProductFreeze 托盘物料冻结
     * @return 结果
     */
    @Override
    public int updateTrayProductFreeze(TrayProductFreeze trayProductFreeze) {
        trayProductFreeze.setUpdateBy(SecurityUtils.getUsername());
        trayProductFreeze.setUpdateTime(DateUtils.getNowDate());
        return trayProductFreezeMapper.updateTrayProductFreeze(trayProductFreeze);
    }

    /**
     * 批量修改托盘物料冻结
     *
     * @param trayProductFreezeList 托盘物料冻结列表
     * @return 结果
     */
    @Override
    public int batchUpdateTrayProductFreeze(List<TrayProductFreeze> trayProductFreezeList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (TrayProductFreeze trayProductFreeze : trayProductFreezeList){
            trayProductFreeze.setUpdateBy(username);
            trayProductFreeze.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<trayProductFreezeList.size();) {
            int endIndex = i+500;
            if (endIndex > trayProductFreezeList.size()) {
                endIndex = trayProductFreezeList.size();
            }
            updateNum = updateNum + trayProductFreezeMapper.batchUpdateTrayProductFreeze(trayProductFreezeList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除托盘物料冻结
     *
     * @param ids 需要删除的托盘物料冻结主键集合
     * @return 结果
     */
    @Override
    public int deleteTrayProductFreezeByIds(Long[] ids) {
        return trayProductFreezeMapper.deleteTrayProductFreezeByIds(ids);
    }

    /**
     * 删除托盘物料冻结信息
     *
     * @param id 托盘物料冻结主键
     * @return 结果
     */
    @Override
    public int deleteTrayProductFreezeById(Long id) {
        return trayProductFreezeMapper.deleteTrayProductFreezeById(id);
    }

    /**
     * 查询托盘物料冻结
     *
     * @param no 编号
     * @return 托盘物料冻结
     */
    @Override
    public TrayProductFreeze selectTrayProductFreezeByNo(String no) {
        return trayProductFreezeMapper.selectTrayProductFreezeByNo(no);
    }

    /**
     * 批量删除托盘物料冻结
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteTrayProductFreezeByNos(String[] nos) {
        return trayProductFreezeMapper.deleteTrayProductFreezeByNos(nos);
    }

    /**
     * 删除托盘物料冻结信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteTrayProductFreezeByNo(String no) {
        return trayProductFreezeMapper.deleteTrayProductFreezeByNo(no);
    }

    /**
     * 根据物料查询冻结集合
     */
    @Override
    public List<TrayProductFreeze> selectTrayProductFreezeListByProductNos(OutStockQueryVo outStockQueryVo){
        return trayProductFreezeMapper.selectTrayProductFreezeListByProductNos(outStockQueryVo);
    }
}
